package com.hue.gulimail.gulimailware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hue.common.utils.R;
import com.hue.gulimail.gulimailware.entity.PurchaseDetailEntity;
import com.hue.gulimail.gulimailware.feign.ProductFeignService;
import com.hue.gulimail.gulimailware.service.PurchaseDetailService;
import com.hue.gulimail.gulimailware.vo.MergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.Query;

import com.hue.gulimail.gulimailware.dao.PurchaseDao;
import com.hue.gulimail.gulimailware.entity.PurchaseEntity;
import com.hue.gulimail.gulimailware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 姚凤桥
 */
@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    private ProductFeignService productFeignService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils unreceiveList() {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.and(w -> {
            w.eq("status", 0).or().eq("status", 1);
        });
        List<PurchaseEntity> list = list(wrapper);
        Page<PurchaseEntity> page = new Page<>();
        page.setRecords(list);
        page.setTotal(list.size());
        page.setCurrent(1);
        page.setSize(10);
        page.setPages(list.size() % 10 == 0 ? list.size() / 10 : list.size() / 10 + 1);
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void merge(MergeVo mergeVo) {
        PurchaseEntity purchase = null;
        if (mergeVo.getPurchaseId() == null) {
            //创建一个新的采购单来整合
            purchase = new PurchaseEntity();
            purchase.setCreateTime(new Date());
            purchase.setUpdateTime(new Date());
            purchase.setPriority(1);
            save(purchase);
        } else {
            purchase = getById(mergeVo.getPurchaseId());
            purchase.setUpdateTime(new Date());
            updateById(purchase);
        }
        //使用选择的采购单来整合
        List<Long> items = mergeVo.getItems();
        PurchaseEntity finalPurchase = purchase;
        List<PurchaseDetailEntity> purchaseDetailList = items.stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = purchaseDetailService.getById(item);
            //设置purchaseId
            purchaseDetailEntity.setPurchaseId(finalPurchase.getId());
            purchaseDetailEntity.setStatus(1);
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        //修改
        purchaseDetailService.updateBatchById(purchaseDetailList);
    }
}
